<?php
    namespace App\Http\Controllers\quickSearch;

    use App\Http\Controllers\Controller;
    use Illuminate\Http\Request;

    class controllerQuickSearch extends Controller
    {
        public function __construct(){
            
            #
            #
            $this->m_gcHelper					= app()->gcHelper				;
        }


        /* 通过关键词快速查找所有数据
         *
         * $search_type : 1=>部门 2=职员 3=>客户 ....
         *
         *
         */
        public function quickSearch($search_type){

            return $this->m_gcHelper->gfApiEchoEx($this->m_gcHelper->gfJsonDecode (

                $this->m_gcHelper->gfGetRequestBodyValue(),
                function($j) use($search_type) {

                    $j->setDecodeStatus($this->quickSearchByType($j,$search_type),'quickSearch.1000',"通过关键词快速查找所有数据 ".$search_type." 成功");
                },
                function($j) use ($search_type) {

                    $j->setDecodeStatus(array(array('json.error : ' => $j->getOriginJsonString())),'quickSearch.2000','通过关键词快速查找所有数据 '.$search_type.' 失败');
                }
            )->getDecodeStatus());	
        }
        private function quickSearchByType($j,$search_type){
            
            # keyword
            #
            $search_keyword	= $j->search_keyword;

            # map search method to get query result
            #
            $ar_map_search_methd	= [
                                            '1'	=> 'quickSearchByTypeInDepartment'	, 
                                            '2'	=> 'quickSearchByTypeInStaff'		,
                                            '3'	=> 'quickSearchByTypeInClient'		,
                                      ];
            $search_method			= @$ar_map_search_methd[$search_type];
            $search_result			= $search_method==''?[]:$this->$search_method($search_keyword);
            
            #
            #
            return	[[
                        'search_keyword'	=> $search_keyword	,
                        'search_type'		=> $search_type		,
                        'search_result'		=> $search_result	,
                    ]];
        }

        /* 在部门表 【 cy_department 】当中查询 department_name
         *
         */
        private function quickSearchByTypeInDepartment($search_keyword){
            
            $this->m_gcHelper->gfDbQuery()->select("select * from ".$this->m_gcHelper->gfGetTableNameByConstKey('CY_DEPARTMENT_TABLE_NAME')." force index(department_name) where department_name like ?",array($search_keyword."%"))->forEachRow(function($row){

                return	[
                            'id'				=> $row->department_id		, # 部门的id
                            'department_name'	=> $row->department_name	, # 部门的名字
                        ];
            },$ar);
            
            return $this->m_gcHelper->gfAutoConvertValueToNumberOrString($ar['content']);
        }
 
        /* 在职员表 【 cy_staff 】当中查询 staff_name
         *
         */
        private function quickSearchByTypeInStaff($search_keyword){
            
            $this->m_gcHelper->gfDbQuery()->select("select * from ".$this->m_gcHelper->gfGetTableNameByConstKey('CY_STAFF_TABLE_NAME')." force index(staff_name) where staff_name like ?",array($search_keyword."%"))->forEachRow(function($row){

                return	[
                            'id'				=> $row->staff_id		    , # 员工id
                            'staff_name'		=> $row->staff_name		    , # 员工名称
                        ];
            },$ar);
            
            return $this->m_gcHelper->gfAutoConvertValueToNumberOrString($ar['content']);
        }

        /* 在客户表 【 cy_client_basic 】当中查询 client_name
         *
         */
        private function quickSearchByTypeInClient($search_keyword){
            
            $this->m_gcHelper->gfDbQuery()->select("select * from ".$this->m_gcHelper->gfGetTableNameByConstKey('CY_CLIENT_BASIC_TABLE_NAME')." force index(client_name) where client_name like ?",array($search_keyword."%"))->forEachRow(function($row){

                    return	[
                                'id'				=> $row->client_id		, # 客户id
                                'client_name'		=> $row->client_name	, # 客户名称
                            ];
                }
                ,
                $ar
            );
            
            #
            #
            return $this->m_gcHelper->gfAutoConvertValueToNumberOrString($ar['content']);
        }
    }
